/****************************************************************************
 *                               RSS3D                            *
 ****************************************************************************
 * Copyright (C) 06/2002
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contact the Author:
 *
 * Claude Fauconnet
 * France
 * mailto: claude.fauconnet@neuf.fr
 *
 ***************************************************************************/
package com.total.reporting.dao;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Mappings.ColumnNames;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public class HibernateUtil {

	private static final SessionFactory sessionFactory;

	static {
		try {
			sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			
			//sessionFactory.getClassMetadata(Personne.class).getEntityName();
		} catch (HibernateException ex) {
			throw new RuntimeException("Exception building SessionFactory: "
					+ ex.getMessage(), ex);
		}
	}

	public static final ThreadLocal session = new ThreadLocal();

	public static Session currentSession() throws HibernateException {
		Session s = (Session) session.get();
		// Open a new Session, if this Thread has none yet
		if (s == null) {
			s = sessionFactory.openSession();
			session.set(s);
		}
		//System.out.println( "----SESSION---"+session.hashCode());
		return s;
	}

	public static void closeSession() throws HibernateException {
		Session s = (Session) session.get();
		session.set(null);
		if (s != null)
			s.close();
	}
	private static final Configuration configuration = new Configuration().configure();

	public static String getColumnName(String className, String propertyName) {
	    PersistentClass persistentClass = configuration.getClassMapping(className);
	    Property property = persistentClass.getProperty(propertyName);
	   return  ((Column)property.getColumnIterator().next()).getName();
	  //  int length = ((ColumnNames) property.getColumnIterator().next()).getLength();

	   // return length;
	}

}